package cn.fbs.config;

import cn.fbs.dao.dm.SysUserMapper;
import cn.fbs.model.dm.SysUser;
import cn.fbs.util.AESUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.java.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Base64;

@Aspect
@Component
@Log
public class AopConfig {
    @Autowired
    private SysUserMapper userMapper;

    @Pointcut("execution(public * cn.fbs.controller.*.*(..))")
    public void requestLog(){}

//    @Pointcut("execution(* cn.fbs.controller.*(..))")
//    public void paramsIntercept(){}

    @Before("requestLog()")
    public void logBefore(JoinPoint joinPoint){
   //        Signature signature = joinPoint.getSignature();
        log.info(
//                "ip:"+request.getRequestURL().toString()+
//                "请求方式:"+request.getMethod()+
//                request.getRemoteAddr()+
           "\n接口:"+joinPoint.getSignature()+
                "\n参数:"+ Arrays.toString(joinPoint.getArgs()));



    }

    @Around("requestLog()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) requestAttributes).getResponse();
        SysUser sysUser = null;
        try {
//            request.getre
            String sessionId = request.getHeader("sessionId");
            if (sessionId == null) {
                response.setHeader("Content-type", "text/html;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write("333,请传sessionId");
                return null;
            }
            sessionId = new String(Base64.getDecoder().decode(sessionId));
            JSONObject jsonObject = JSONObject.parseObject(sessionId);
            String loginName = jsonObject.get("loginName").toString();
            String password = AESUtils.decryptData(jsonObject.get("password").toString());
            sysUser = userMapper.selectByLoginName(loginName);
            if (sysUser==null){
                response.setHeader("Content-type", "text/html;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write("333,用户名不存在");
            }
            String myPassword = sysUser.getPassword();
            if (!password.equals(myPassword)) {
                response.setHeader("Content-type", "text/html;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write("333,密码错误");
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write("333,登录失败");
            return null;
        }
        HttpSession session = request.getSession();
        session.setAttribute("userId", sysUser.getUserId());
        session.setAttribute("userName",sysUser.getUserName());
        session.setAttribute("loginName",sysUser.getLoginName());
        session.setAttribute("userRole",sysUser.getUserRole());
        return joinPoint.proceed();

    }


}
